package com.flx.sys;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import com.flx.sys.jwt.dto.JwtUser;

@Component
public class JwtUserDetailsService implements UserDetailsService {

	@Autowired
	private PasswordEncoder passwordEncoder;
	final SimpleDateFormat sl = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		Date date = null;
		try {
			date = sl.parse("2018-04-19 11:10:10");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JwtUser jwtUser = new JwtUser("1", username, passwordEncoder.encode("123456"), "zhangkaisx@126.com",
				AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"), date);
		if (jwtUser == null) {
			throw new UsernameNotFoundException("没有找到该用户");
		} else {
			return jwtUser;
		}
	}

	public static void main(String[] args) {
		SimpleDateFormat sl = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
		System.out.println(sl.format(new Date(System.currentTimeMillis() + 1 * 360000)));
	}

}
